package com.example.lenovo.telephonerecord;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

import java.security.PrivilegedAction;

/**
 * Created by Lenovo on 2017/10/20.
 */

public class RecordService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        //  创建一个电话管理器对象，通过这个对象可以监听电话的状态
        TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        MyPhoneStateListener listener = new MyPhoneStateListener();
        //  调用电话管理器的listen方法，注册监听
        manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
        //--------------------------录音需要权限--------------------

    }
    private class MyPhoneStateListener extends PhoneStateListener{
        MediaRecorder recorder = null;
        public void onCallStateChanged(int state,String incomingNumber) {
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:      //空闲状态（录音结束）
                    if(recorder != null) {
                        try{
                            recorder.stop();
                            recorder.reset();
                            recorder.release();
                        } catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                    break;
                case TelephonyManager.CALL_STATE_RINGING:       //  响铃（准备录音机）
                    try{
                        recorder = new MediaRecorder();
                        //  设置音频的输入源，MIC：录自己的声音 voice_call录双方的声音
                        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                        //  设置音频的输出格式  3GP
                        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        //  设置音频编码 amr：彩铃手机音频格式  NB：窄带  WB：宽带
                        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        //  设置输出之后的文件保存的路径
                        recorder.setOutputFile(getCacheDir()+"/" +incomingNumber+".3pg");
                        //  录音机开始准备
                        recorder.prepare();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:   //  接电话了（开始录音）
                    recorder.start();
                    break;
            }
        }
    }


    @Override
    public int onStartCommand(Intent intent,  int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
